2018 Fall 期末專案示範網站

GUI 程式範例 << Previous Next >> parse_content 範例

GUI 程式碼

擷取網站 html 檔案

def render(url):
    """Fully render HTML, JavaScript and all."""

    import sys
    from PyQt5.QtCore import QEventLoop,QUrl
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtWebEngineWidgets import QWebEngineView

    class Render(QWebEngineView):
        def __init__(self, url):
            self.html = None
            self.app = QApplication(sys.argv)
            QWebEngineView.__init__(self)
            self.loadFinished.connect(self._loadFinished)
            self.load(QUrl(url))
            while self.html is None:
                self.app.processEvents(QEventLoop.ExcludeUserInputEvents | QEventLoop.ExcludeSocketNotifiers | QEventLoop.WaitForMoreEvents)
            self.app.quit()

        def _callable(self, data):
            self.html = data

        def _loadFinished(self, result):
            self.page().toHtml(self._callable)

    return Render(url).html

dummy_url = "http://mde.tw/"
print(render(dummy_url))

顯示網站內容

# coding: utf-8

import sys
import os

from PyQt5 import (
    QtCore, 
    QtWidgets, 
    QtWebEngineWidgets
)

app = QtWidgets.QApplication(sys.argv)
view = QtWebEngineWidgets.QWebEngineView()

'''
view.load(QtCore.QUrl().fromLocalFile(
    os.path.split(os.path.abspath(__file__))[0]+r'\html\test.html'
))
'''
view.load(QtCore.QUrl("http://mde.tw"))

view.show()
sys.exit(app.exec())


GUI 程式範例 << Previous Next >> parse_content 範例